home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / zendisk2.zip / LST13-12.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  2KB  |  77 lines

  1. ;
  2. ; *** Listing 13-12 ***
  3. ;
  4. ; Determines whether there are more non-negative or negative
  5. ; elements in an array of 8-bit signed values, using
  6. ; duplicated code with two LOOP instructions and two RET
  7. ; instructions.
  8. ;
  9.     jmp    Skip
  10. ;
  11. ARRAY_LENGTH    equ    256
  12. ByteArray    label    byte
  13. X=0
  14.     rept    ARRAY_LENGTH
  15.     db    X
  16. X=X+1
  17.     endm
  18. ;
  19. ; Determines whether there are more non-negative or
  20. ; negative elements in the specified array of 8-bit
  21. ; signed values.
  22. ;
  23. ; Input:
  24. ;    CX = length of array
  25. ;    DS:SI = array to check
  26. ;
  27. ; Output:
  28. ;    DX = signed count of the number of non-negative
  29. ;        elements found in the array minus the number
  30. ;        of negative elements found. (Zero if there
  31. ;        are the same number of each type of element.
  32. ;        Otherwise, sign bit set if there are more
  33. ;        negative elements than non-negative
  34. ;        elements, cleared if there are more
  35. ;        non-negative elements than negative
  36. ;        elements)
  37. ;
  38. ; Registers altered: AL, CX, DX, SI
  39. ;
  40. ; Direction flag cleared
  41. ;
  42. ; Note: Only useful if the surplus of non-negative
  43. ;    elements over negative elements is less than
  44. ;    32K, or if the surplus of negative elements
  45. ;    over non-negative elements is less than or
  46. ;    equal to 32K. Otherwise, the signed count
  47. ;    returned in DX overflows.
  48. ;
  49. ; Note: Does not handle arrays that are longer than 64K
  50. ;    bytes or cross segment boundaries.
  51. ;
  52. CountNegPos:
  53.     cld
  54.     sub    dx,dx    ;initialize the count to zero
  55. CountNegPosLoop:
  56.     lodsb        ;get the next byte to check
  57.     and    al,al    ;see if it's negative or
  58.             ; non-negative 
  59.     js    CountNeg ;it's negative
  60.     inc    dx    ;count off one non-negative element
  61.     loop    CountNegPosLoop
  62.     ret
  63. CountNeg:
  64.     dec    dx    ;count off one negative element
  65.     loop    CountNegPosLoop
  66.     ret
  67. ;
  68. Skip:
  69.     call    ZTimerOn
  70.     mov    si,offset ByteArray    ;array to check
  71.     mov    cx,ARRAY_LENGTH        ;# of bytes to check
  72.     call    CountNegPos        ;see whether there
  73.                     ; are more negative
  74.                     ; or non-negative
  75.                     ; elements
  76.     call    ZTimerOff
  77.